library(testthat)
context("avm test3")

test_that("avm test3", {
    #建立数据库连接
    con<-dbconnect2()
    # 读取基础数据
    tabln.vec<-loadData(con=con,month_offset = -2)
    tabln.vec$ha_info.sp<-tabln.vec$ha_info.sp%>%remove.spatial.outlier(city_code='bj')
    # 建立poi相关信息
    poi.data.list<-getpoi.ras(con=con,tabln.vec = tabln.vec)
    # 取出得到POI信息的小区信息
    tabln.vec$ha_info.sp<-poi.data.list$ha_info.sp
    # 准备建模数据
    data.sets.all<-avm.data.train(tabln.vec,proptype = '11')
    # 取出房源交易数据,并排除一些字段
    data.deal.sale<-read.deal(con = con,year = 2017,month = 7,proptype = 11,
                              var.except = c('ID','offertm','bldgcode','bheight','floor','lr','strucode',
                                             'facecode','int_deco','propRT','dealcode'))
    # 关联交易与小区
    data.deal.sale.ha<-merge(data.deal.sale,data.sets.all,by='ha_code')
    # 分割训练集和测试集
    data.sets<-data.split(data =data.deal.sale.ha,train.rate = 0.75)
    # 房源建模时的排除变量
    var.except.deal<-'-ID-offertm-unitprice-proptype-bldgcode-bheight-floor-lr-strucode-facecode-int_deco-propRT-dealcode'
    # 建模, 模拟房源房价
    avm.models<-avm.fit(data.train = data.sets$train,dependentY = 'unitprice',ntree = 500,nround = 1000,
                        independentX = paste0('~.-yearmonth-ymid-ha_code-volume_rate-greening_rate',
                                              '-x_-y_-saleprice-rentprice-unitprice',
                                              '-rentbldgarea-salebldgarea-bldg_type-bldg_code',
                                              var.except.deal
                                              ),
                        remove.dup=F
                        )


    # 测试集预测
    pred.test.data<-avm.pred(train.models=avm.models,newdata=data.sets$test%>%na.omit(),testing = T,remove.dup=F)
    # sapply(pred.test.data$pred%>%head,exp)
    pred.test.data$rmse
    pred.test.data$mae
    # 保存路径，默认为'./models.avm'
    model.path<-model.store.path()
    # 模型保存
    avm.model.store(city_code='bj',avm.models = avm.models,
                    yearmonth = '2017-7',proptype = '11',dependentY = 'unitprice_sale')

    # 保存相关数据
    saveRDS(tabln.vec,file.path(model.path,paste('bj','2017-7','tab.rds',sep='_')))
    saveRDS(poi.data.list,file.path(model.path,paste('bj','2017-7','poi.rds',sep='_')))

    ###############################################################################################################

    # 模型读取
    avm.models.2<-avm.model.restore(city_code='bj',yearmonth = '2017-7',proptype = '11',
                                    dependentY = 'unitprice_sale')
    # 保存路径，默认为'./models.avm'
    model.path<-model.store.path()
    # 读取数据
    tabln.vec <- readRDS(file.path(model.path,paste('bj','2017-7','tab.rds',sep='_')))
    poi.data.list <- readRDS(file.path(model.path,paste('bj','2017-7','poi.rds',sep='_')))

    # 生成未知预测数据,
    pred.xy<-avm.pred.data(train.models = avm.models.2,
                           x=116.4411,
                           y=39.848,
                           tabln.vec = tabln.vec,poi.data.list = poi.data.list)
    # data.sets$train[1,]
    # 用读取的模型再次预测
    avm.pred(train.models=avm.models.2,newdata=pred.xy,remove.dup = F)
    # 关闭所有数据库连接
    killDbConnections()
})
